//
//  XYJJYViewController.m
//  徙游记-01
//
//  Created by hcios on 2017/7/16.
//  Copyright © 2017年 鹿林. All rights reserved.
//

#import "XYJJYViewController.h"
#import "SZKImagePickerVC.h"

@interface XYJJYViewController ()<SZKImagePickerVCDelegate,UITextViewDelegate>{
    
    NSMutableArray *imagesArray;
    NSMutableDictionary *imageDict;
}

@property (nonatomic,copy) NSString *stringType;
@property (nonatomic,strong) UITapGestureRecognizer *ges;

@end

@implementation XYJJYViewController

#pragma mark - init
-(instancetype)init{
    if (self = [super init]) {
        imagesArray = [[NSMutableArray alloc] init];
        imageDict = [[NSMutableDictionary alloc] init];
    }
    return self;
}


#pragma mark - 生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
   
    self.view.frame = RECT_MAKE(0, 60, SCREEN_WIDTH, SCREEN_HEIGHT);
    self.view.backgroundColor = RGB(235, 235, 241);
    self.title = @"建议";
    self.view.userInteractionEnabled = YES;
    self.ges = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideTextView)];
    //初始化视图
    [self setupSubViews];
}

-(void)hideTextView{
    [_suggestTextView resignFirstResponder];
}


#pragma mark - 初始化视图
-(void)setupSubViews{
    _suggestTextView.layer.cornerRadius = 4;
    _suggestTextView.layer.masksToBounds = YES;
    _suggestTextView.text = @"请提出您的意见与建议";
    [_suggestTextView setTextColor:RGB(214, 226, 238)];
    _suggestTextView.backgroundColor = [UIColor whiteColor];
    _suggestTextView.delegate = self;
    
    _addImgView.layer.cornerRadius = 3;
    _addImgView.layer.masksToBounds = YES;
    _addImgView.userInteractionEnabled = YES;
    _addImgView.backgroundColor = [UIColor whiteColor];
    UITapGestureRecognizer *addGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(addImg)];
    [_addImgView addGestureRecognizer:addGes];
    
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"提交" style:UIBarButtonItemStylePlain target:self action:@selector(sendSuggest)];
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
}


#pragma mark - 点击事件
//显示分享view
-(void)sendSuggest{
    [self alter];
}


#pragma mark - textViewDelegate
-(void)
textViewDidEndEditing:(UITextView *)textView{
    [self.view removeGestureRecognizer:self.ges];
    if (_suggestTextView.text.length > 0) {
        
    }else{
        [_suggestTextView setTextColor:RGB(235, 235, 241)];
        _suggestTextView.text = @"请提出您的意见与建议";
    }
}


-(void)textViewDidBeginEditing:(UITextView *)textView{
    [self.view addGestureRecognizer:self.ges];
    _suggestTextView.text = nil;
    [_suggestTextView setTextColor:[UIColor blackColor]];
}


#pragma mark - 添加图片点击事件
-(void)addImg{
    UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction *camera = [UIAlertAction actionWithTitle:@"打开相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            [self presentViewController:ImagePickerStyleCamera];
        }else{
            NSLog(@"该设备不支持相机功能");
        }
    }];
    
    UIAlertAction *photo = [UIAlertAction actionWithTitle:@"打开相册" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        [self presentViewController:ImagePickerStylePhotoLibrary];
    }];
    
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    
    [alertC addAction:camera];
    [alertC addAction:photo];
    [alertC addAction:cancel];
    
    [self presentViewController:alertC animated:YES completion:nil];
}

-(void)presentViewController:(imagePickerStyle)style{
    SZKImagePickerVC *picker = [[SZKImagePickerVC alloc] initWithImagePickerStyle:style];
    picker.SZKDelegate = self;
    [self presentViewController:picker animated:YES completion:^{
        
    }];
}


#pragma mark - SZKImagePickerVCDelegate
-(void)imageChooseFinish:(UIImage *)image{
    [_addImgView setImage:image];
    //保存到沙盒中
    [SZKImagePickerVC saveImageToSandbox:image andImageNage:@"addIamge" andResultBlock:^(BOOL success) {
        NSLog(@"保存成功");
        [imagesArray addObject:image];
    }];
    [self addImageF];
}



#pragma mark - 图片处理，压缩图片
-(void)addImageF{
    _addImgView.image = imagesArray[0];
    NSData *imgData = UIImagePNGRepresentation(_addImgView.image);
    if (imgData.length > 1024000) { //图片不大于1M
        CGSize imgSize = CGSizeMake(_addImgView.image.size.width / 2, _addImgView.image.size.height / 2);
        _addImgView.image = [self OriginImage:_addImgView.image scaleToSize:imgSize];   //压缩图片尺寸
    }
    [imageDict setObject:_addImgView.image forKey:[NSString stringWithFormat:@"imageData"]]; //后台让你传的图片对应的key
}


//图片处理，图片压缩
-(UIImage *)OriginImage:(UIImage *)image scaleToSize:(CGSize)size{
    UIGraphicsBeginImageContext(size);  //你所需要的尺寸
    [image drawInRect:CGRectMake(0, 0, size.width,size.height)];
    UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return scaledImage; //返回已变图片
}


#pragma mark - 按钮的选择
-(IBAction)selectedType:(UIButton *)sender{
    if (sender.tag == 1) {
        [_projectBtn setImage:[UIImage imageNamed:@"feedback_selected"] forState:UIControlStateNormal];
        [_ableBtn setImage:[UIImage imageNamed:@"feedback_unSelected"] forState:UIControlStateNormal];
        _stringType = @"程序错误";
         }else if(sender.tag == 2){
             [_ableBtn setImage:[UIImage imageNamed:@"feedback_selected"] forState:UIControlStateNormal];
             [_projectBtn setImage:[UIImage imageNamed:@"feedback_unSelected"] forState:UIControlStateNormal];
             _stringType = @"功能建议";
         }
}

#pragma mark - alter提示方法
-(void)alter{
    UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:[NSString stringWithFormat:@"您的意见已提交，我们会尽快作出答复"] preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alertC addAction:sureAction];
    [self.navigationController presentViewController:alertC animated:YES completion:nil];
}


@end
